using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace HybridCLR.Extension.Editor
{
    public partial class  Commond
    {
        [UnityEditor.MenuItem("HybridCLR/Extension/CopyHotDllToPath_Test")]
        public static void CopyHotDllToPath_Test()
        {
            CopyHotDllToPath();
        }

        public static void CopyHotDllToPath(BuildParam buildParam)
        {
            CopyHotDllToPath(buildParam.CopyDllToPath);
        }
        
        public static void CopyHotDllToPath(string copyToPath = "Assets/HybridCLRGenerate/Dll")
        {
            if(!System.IO.Directory.Exists(copyToPath))
                System.IO.Directory.CreateDirectory(copyToPath);
            var setting = HybridCLR.Editor.Settings.HybridCLRSettings.Instance;
            HotDllConfig config = new HotDllConfig();
            foreach (var e in setting.hotUpdateAssemblyDefinitions)
            {
                string oldFilePath = System.IO.Path.Combine(HybridCLR.Editor.SettingsUtil.GetHotUpdateDllsOutputDirByTarget(UnityEditor.EditorUserBuildSettings.activeBuildTarget), e.name + ".dll");
                string newFilePath = System.IO.Path.Combine(copyToPath, e.name + ".dll.bytes");
                System.IO.File.Copy(oldFilePath, newFilePath, true);
                config.hotUpdateAssemblyDefinitions.Add(e.name);
            }
            foreach (var e in setting.hotUpdateAssemblies)
            {
                string oldFilePath = System.IO.Path.Combine(HybridCLR.Editor.SettingsUtil.GetHotUpdateDllsOutputDirByTarget(UnityEditor.EditorUserBuildSettings.activeBuildTarget), e + ".dll");
                string newFilePath = System.IO.Path.Combine(copyToPath, e + ".dll.bytes");
                System.IO.File.Copy(oldFilePath, newFilePath, true);
                config.hotUpdateAssemblies.Add(e);
            }

            string jsonContent = UnityEngine.JsonUtility.ToJson(config);
            System.IO.File.WriteAllText(System.IO.Path.Combine(copyToPath, "HotDllConfig.json"), jsonContent);
            UnityEditor.AssetDatabase.SaveAssets();
            UnityEditor.AssetDatabase.Refresh();
        }

        
    }
}
